package com.mk.cloud.pt.controller.app;

import act.apidoc.Description;
import act.controller.Controller;
import act.handler.NonBlock;
import act.inject.DefaultValue;
import act.util.CacheFor;
import com.mk.cloud.common.domain.PBrand;
import com.mk.cloud.common.domain.PProduct;
import com.mk.cloud.common.enums.GlobalValues;
import com.mk.cloud.common.interceptor.header.HeaderService;
import com.mk.cloud.common.interceptor.header.IHLogin;
import com.mk.cloud.common.util.ResUtils;
import com.mk.cloud.pt.service.app.PCompanyService;
import org.beetl.sql.core.engine.PageQuery;
import org.osgl.http.H;
import org.osgl.mvc.annotation.GetAction;
import org.osgl.mvc.annotation.With;
import org.osgl.util.S;

import javax.inject.Inject;
import java.util.HashMap;
import java.util.Map;

@Controller("app/product")
public class PProductController extends HeaderService {

    @Inject
    private PProduct.Mapper pProductMapper;
    @Inject
    private PBrand.Mapper pBrandMapper;
    @Inject
    private PCompanyService pCompanyService;

    @Description("商品--商品列表(微信小程序查询商品列表)")
    @GetAction("pageListApp.json")
//    @With(IHLogin.class)
    @CacheFor(value = 10 * 60)
    @NonBlock
    public String pProductListApp(
                                  @Description("小程序appid") String appid,
                                  @Description("筛选条件-新店体验商品id") String ppdid,
                                  @Description("筛选条件-商品类型") String pdtype,
//            @Description("筛选条件-商品所属公司") String coid,
                                  @Description("当前第几页") @DefaultValue("1") Integer page,
                                  @Description("每页多少条") @DefaultValue("15") Integer size

    ) {
        PageQuery<PProduct> query = new PageQuery<>();
        query.setPageSize(size);
        query.setPageNumber(page);
        Map<String, Object> map = new HashMap<>();
        map.put("ppdid", ppdid);
        map.put("pdstatus", GlobalValues.PD_STATUS_2.value());
        map.put("pdtype", pdtype);
        map.put("coid", pCompanyService.getCompanyByAppid(appid).getCoid());
        query.setParas(map);
        //此处的sql是三个sql
        pProductMapper.getSQLManager().pageQuery("pProduct.select_product_page_list", PProduct.class, query);
        return ResUtils.OK(query.getList());
    }

//    @Description("商品--某类型商品的单个商品")
//    @GetAction("typepddetail.json")
//    public String pProductDetail(H.Session session,
//          @Description("筛选条件-新店体验商品id") String ppdid,
//          @Description("筛选条件-商品类型") String pdtype,
//          @Description("当前第几页") @DefaultValue("1") Integer currentPage,
//          @Description("每页多少条") @DefaultValue("1") Integer pageSize
//
//    ) {
//        PageQuery<PProduct> query = new PageQuery<>();
//        query.setPageSize(pageSize);
//        query.setPageNumber(currentPage);
//        Map<String, Object> map = new HashMap<>();
//        map.put("ppdid", ppdid);
//        map.put("pdstatus", GlobalValues.PD_STATUS_2.value());
//        map.put("pdtype", pdtype);
//        map.put("coid", session.get("coid"));
//        query.setParas(map);
//        //此处的sql是三个sql
//        pProductMapper.getSQLManager().pageQuery("pProduct.select_product_page_list", PProduct.class, query);
//        return ResUtils.OK(query.getList());
//    }

    @Description("商品--商品详情")
    @GetAction("get.json")
    @CacheFor(value = 10 * 60)
    @NonBlock
    public String getPProduct(@Description("商品id") String pdid) {
        if (S.isEmpty(pdid)) {
            return ResUtils.OK(new PProduct());
        }
        PProduct product = pProductMapper.single(pdid);
        if(product!=null&&S.isNotBlank(product.getBrid())){
            PBrand brand = pBrandMapper.single(product.getBrid());
            if(brand!=null){
                product.setBrname(brand.getBrname());
            }
        }
        return ResUtils.OK(product);
    }

}
